package com.dm.wallpaper.library.helpers;


import com.dm.wallpaper.library.ResourceTable;
import com.dm.wallpaper.library.utils.Log;
import com.dm.wallpaper.library.utils.SystemUtils;
import ohos.agp.render.render3d.BuildConfig;
import ohos.app.Context;
import ohos.bundle.BundleInfo;
import ohos.rpc.RemoteException;

import java.io.*;

/*
 * Wallpaper Board
 *
 * Copyright (c) 2017 Dani Mahardhika
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class CrashReportHelper {

    public static String buildCrashLog(Context context, File folder, String stackTrace) throws IOException {
        File fileDir = new File(folder.toString() + "/crashlog.txt");
        Writer out = null;
        try {
            if (stackTrace.length() == 0) return null;

            String deviceInfo = getDeviceInfoForCrashReport(context);
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(fileDir), "UTF8"));
            out.append(deviceInfo).append(stackTrace);
            out.flush();

            return fileDir.toString();
        } catch (Exception | OutOfMemoryError e) {
            Log.e("", e.toString());
        } finally {
            if (out != null) {
                out.close();
            }
        }
        return null;
    }

    private static String getDeviceInfo(Context context) {
        StringBuilder sb = new StringBuilder();
        final int height = SystemUtils.getDisplayHightInPx(context);
        final int width = SystemUtils.getDisplayWidthInPx(context);

        String appVersion = "";
        try {
            BundleInfo bundleInfo = context.getBundleManager().getBundleInfo(context.getBundleName(), 0);
            appVersion = bundleInfo.getVersionName();
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        sb
//                .append("Manufacturer : ").append(Build.MANUFACTURER)
//                .append("\nModel : ").append(Build.MODEL)
//                .append("\nProduct : ").append(Build.PRODUCT)
                .append("\nScreen Resolution : ")
                .append(width).append(" x ").append(height).append(" pixels")
                .append("\nApp Version : ").append(appVersion)
                .append("\n");
        return sb.toString();
    }

    public static String getDeviceInfoForCrashReport(Context context) {
        return "WallpaperBoard Version : " + BuildConfig.VERSION_NAME
                + "\nApp Name : "
                + SystemUtils.getString(context, ResourceTable.String_app_name)
                + "\n" + getDeviceInfo(context);
    }
}
